﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Web;


namespace MySiteCreatorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                
                string siteURL;
                string userName;


                Console.WriteLine("Enter the URL of the MySite host site");
                siteURL = Console.ReadLine();
                Console.WriteLine("Enter the users you wish to create sites for, seperated by ;...");
                userName = Console.ReadLine();
                                               
                using (SPSite spSite = new SPSite(siteURL))
                {

                    ServerContext siteContext = ServerContext.GetContext(spSite);
                    UserProfileManager pmManager = new UserProfileManager(siteContext);

                    string[] usernames = userName.Split(';');
                    foreach (string strUserName in usernames)
                    {
                        UserProfile spUser = null;
                        if (!pmManager.UserExists(strUserName))
                        {
                            Console.WriteLine("User does not exist, creating...");
                            spUser = pmManager.CreateUserProfile(strUserName);
                            spUser.Commit();

                        }
                        spUser = pmManager.GetUserProfile(strUserName);
                        Console.WriteLine("Creating MySite for user {0}, this may take a few minutes.", userName);
                        spUser.CreatePersonalSite();
                        spUser.Commit();
                    }


                }

            });
        }
        

        }
    }
